package com.baidu.controller;

import com.baidu.pojo.GoodsBean;
import com.baidu.pojo.PicBean;
import com.baidu.service.GoodsService;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 创作时间：2019/10/25 11:28
 * 作者：李增强
 */
@Controller
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @RequestMapping("/saveGoodsPic")
    public String saveGoodsPic(Integer gid, @Param("file") MultipartFile file, HttpServletRequest request) {
        if(!file.isEmpty()){
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            df.format(new Date());
            String uuid = UUID.randomUUID().toString().replace("-", "");
            uuid = uuid.substring(0, 4);

            /**
             * 这个是往数据库保存的路径
             */
            String path = "image/" + df.format(new Date()) + "/" + uuid + "/" + file.getOriginalFilename();
            /**
             * 这是项目的真实路径（webapp），我们数据库保存的时候，不需要这个路径
             * 数据库保存的需要是的相对路径，因为我们在读流的时候，需要项目的真实路径加上相对路径
             */
            String realPath = request.getSession().getServletContext().getRealPath("/");
            String filepath = realPath + path;
            File flie1 = new File(filepath);
            try {
                FileUtils.copyInputStreamToFile(file.getInputStream(), flie1);
                path="http://localhost:8888/cloudmanage/"+path;
                goodsService.savePic(gid,path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "redirect:toUpdateGoodsPic.do?gid=" + gid;
    }

    /**
     * 删除图片，先只把数据删掉
     */
    @RequestMapping("/deletePicByPid")
    @ResponseBody
    public void deletePicByPid(Integer pid, HttpServletRequest request, HttpServletResponse response) {
        String realPath = request.getSession().getServletContext().getRealPath("/");
        PicBean pb = goodsService.getPicBeanByPid(pid);
        realPath += pb.getPurl();
        goodsService.deletePicByPid(pid);
        File file = new File(realPath);
        file.delete();

        try {
            response.getWriter().write("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/toUpdateGoodsPic")
    public String toUpdateGoodsPic(Integer gid, Model model) {
        List<PicBean> list = goodsService.getGoodsPicList(gid);
        model.addAttribute("gid", gid);
        model.addAttribute("list", list);
        return "goods_pic";
    }


    @RequestMapping("/getGoodsListLine")
    public String getGoodsListLine(Integer gstatus, Model model) {
        List<GoodsBean> list = goodsService.getGoodsListByGstatus(gstatus);
        model.addAttribute("list", list);
        return "goods_line";
    }

    /**
     * 商品的上架和下架
     */
    @RequestMapping
    public String updateGoodsStatus(Integer gid) {
        goodsService.updateGoodsStatus(gid);
        return "redirect:getGoodsList.do";
    }


    @RequestMapping("/getGoodsList")
    public String getGoodsList(Model model) {
        List<GoodsBean> list = goodsService.getGoodsList();
        model.addAttribute("list", list);
        return "goods_list";
    }
}
